package cn.kuick.study.utils;

import java.io.IOException;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HttpUtils {
	private static final Log log = LogFactory.getLog(HttpUtils.class);
	
	public static String doGet(String url){
        HttpClient client =  new HttpClient();
        
        GetMethod method = new GetMethod(url);  

        try {
			client.executeMethod(method);
			return method.getResponseBodyAsString();
		} catch (IOException e) {
			log.error("-----url路径有误或其他错误" + e + "--------");
		}  

		return null;
	}
	
	public static String doPost(String url, Map<String,String> params){
        HttpClient client =  new HttpClient();
        
        client.getParams().setContentCharset("UTF-8");
        PostMethod method = new PostMethod(url); 

		if(params != null) {
			for (Map.Entry<String, String> entry : params.entrySet()) {
				String key = entry.getKey();
				String value = entry.getValue();
				method.addParameter(key, value);
			}
		}

        try {
			client.executeMethod(method);
			return method.getResponseBodyAsString();
		} catch (IOException e) {
			log.error("-----url路径有误或其他错误" + e + "--------");
		}  

		return null;
	}
}
